﻿@page "/xlsio/worksheet-to-image"
@inject Microsoft.JSInterop.IJSRuntime JS
@inject Microsoft.AspNetCore.Hosting.IWebHostEnvironment hostingEnvironment
@using System.IO;

@using Syncfusion.Blazor.Buttons
@*Hidden:Lines*@

@using BlazorDemos.Data.FileFormats.XlsIO

@*End:Hidden*@
@inherits SampleBaseComponent;

<SampleDescription>
   <p>This sample demonstrates how to convert spreadsheets to image using XlsIO.</p> 
</SampleDescription>
<ActionDescription>
<p>A worksheet can be converted to an image that is subject to few limitations. The image can be of any format, and it can also be saved as a memory stream.</p>
<p style='display: block'><b>Features:</b></p>
<ul>
<li>Worksheet to image conversion</li>
<li>Saving workbooks as images of Bitmaps and metafiles</li>
</ul>
</ActionDescription>

<div class="control-section">
    <p style="font-weight:normal">Click the button below to convert the Excel worksheet to image using Essential XlsIO.</p>

    <div class="radio-control">
        <h5 class="row">Image Format:</h5>
        <div class="e-radio-wrapper">
            <SfRadioButton Label="PNG" Name="PNG" Value=@("PNG") @bind-Checked="@imageFormat"></SfRadioButton>
        </div>
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <div class="e-radio-wrapper">
            <SfRadioButton Label="JPEG" Name="JPEG" Value=@("JPEG") @bind-Checked="@imageFormat"></SfRadioButton>
        </div>
    </div>
    <div class="button-section">
        <div id="button-control">
            <div class="row">
                <div>
                    <SfButton @onclick="InputDocument">Input Document</SfButton>&nbsp;
					<SfButton @onclick="Convert">Create Document</SfButton>
                </div>
            </div>
        </div>
    </div>
</div>

<style>
    .radio-control {
        margin: 5% 0 2% 0;
    }

    .control-section .row {
        margin: 10px 0;
    }
</style>

@code {
    MemoryStream excelStream;
    string imageFormat = "PNG";
    string button = string.Empty;

    /// <summary>
    /// Download the Excel document used for conversion
    /// </summary>
    public async Task InputDocument()
    {
        WorksheetToImageService service = new WorksheetToImageService(hostingEnvironment);
        button = "Input Document";
        excelStream = service.WorksheetToImageXlsIO(button, imageFormat);
        await JS.SaveAs("Template.xlsx", excelStream.ToArray());
    }

    /// <summary>
    /// Convert the Excel document to image
    /// </summary>
    public async Task Convert()
    {
        WorksheetToImageService service = new WorksheetToImageService(hostingEnvironment);
        button = "Convert";
        excelStream = service.WorksheetToImageXlsIO(button, imageFormat);
        if (imageFormat == "PNG")
        {
            await JS.SaveAs("Image.png", excelStream.ToArray());
        }
        else
        {
            await JS.SaveAs("Image.jpeg", excelStream.ToArray());
        }
    }    
}
